﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HDTuyenChon.Form;
using HDTuyenChon.BL;
using System.IO;
using HDTuyenChon.AppCode;

namespace HDTuyenChon.Admin
{
	public partial class InsertPhim : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (Session["TaiKhoan"] == null)
			{
				Response.Redirect("~/Default.aspx");
				return;
			}
			//Init combo value
			if(!Page.IsPostBack)
			{
				CommonBL blCommon = new CommonBL();
				ddlLoaiPhim.Items.AddRange(blCommon.GetComboData("LOAI_PHIM"));
				ddlLoaiHD.Items.AddRange(blCommon.GetComboData("LOAI_HD"));
				ddlNamSX.Items.AddRange(blCommon.GetComboData("NAM_SX"));				
			}

			PhimBL blPhim = new PhimBL();
			txtMaPhim.Text =  blPhim.CreateMaPhim();
			lblMessage.Text = "";
			txtTenPhimVN.Focus();
		}

		protected void btnInsert_Click(object sender, EventArgs e)
		{										
			//Insert values
			Phim phim = new Phim();
			phim.MS_Phim = txtMaPhim.Text;
			phim.Tua_VN = txtTenPhimVN.Text;
			phim.Tua_EN = txtTenPhimEN.Text;
			phim.LoaiPhim = ddlLoaiPhim.SelectedItem.Value;
			phim.Dung_Luong = Convert.ToInt32(txtDungLuong.Text);
			phim.ChieuRap = chkPhimChieuRap.Checked;
			phim.LoaiHD = ddlLoaiHD.SelectedItem.Value;
			phim.Nam_SX = ddlNamSX.SelectedItem.Value;			
			//Upload hinh
			if (flHinhAnh.HasFile)
			{
				ImageUpload imageUpload = null;
				if (flHinhAnh.PostedFile.ContentType == "image/jpeg" || flHinhAnh.PostedFile.ContentType == "image/png")
				{
					imageUpload = new ImageUpload();
					imageUpload.HandleUploadedFileUseExistingName("Images", txtTenPhimEN.Text +"_hdchonloc" + ".jpg");
					phim.Hinh_Anh = txtTenPhimEN.Text + "_hdchonloc";
				}				
				else
				{
					lblMessage.Text = "Loại hình ảnh không hỗ trợ hiển thị trên web!";
					return;
				}
			}		

			PhimBL blPhim = new PhimBL();
			if (blPhim.Insert(phim, (TaiKhoan)Session["TaiKhoan"]) > 0)
			{
				lblMessage.Text = "Thêm thành công!";
			}
			else
			{
				lblMessage.Text = "Thêm thất bại!";
			}

			phim.Link1 = txtLink1.Text.Trim();
			phim.Link2 = txtLink2.Text.Trim();
			phim.Link3 = txtLink3.Text.Trim();
			phim.Link4 = txtLink4.Text.Trim();
			phim.Link5 = txtLink5.Text.Trim();
			phim.Link6 = txtLink6.Text.Trim();
			phim.Link7 = txtLink7.Text.Trim();
			phim.Link8 = txtLink8.Text.Trim();
			phim.Link9 = txtLink9.Text.Trim();
			phim.Link10= txtLink10.Text.Trim();
			phim.Link11 = txtLink11.Text.Trim();
			phim.Link12 = txtLink12.Text.Trim();
			phim.Link13 = txtLink13.Text.Trim();
			phim.Link14 = txtLink14.Text.Trim();
			phim.Link15 = txtLink15.Text.Trim();

			txtTenPhimVN.Text = "";
			txtTenPhimEN.Text = "";
			txtDungLuong.Text = "";
			txtMaPhim.Text = blPhim.CreateMaPhim();
		}

		private void Clear()
		{
			txtMaPhim.Text = "";
			txtTenPhimVN.Text = "";
			txtTenPhimEN.Text = "";
			ddlLoaiPhim.SelectedIndex = 0;
			txtDungLuong.Text = "";
			ddlLoaiHD.SelectedIndex = 0;
			ddlNamSX.SelectedIndex = 0;			
			txtTenPhimVN.Focus();

			txtLink1.Text= "";
			txtLink2.Text= "";
			txtLink3.Text= "";
			txtLink4.Text= "";
			txtLink5.Text= "";
			txtLink6.Text= "";
			txtLink7.Text= "";
			txtLink8.Text= "";
			txtLink9.Text= "";
			txtLink10.Text= "";
			txtLink11.Text= "";
			txtLink12.Text= "";
			txtLink13.Text= "";
			txtLink14.Text= "";
			txtLink15.Text= "";
			txtLink16.Text = "";
			txtLink17.Text = "";
			txtLink18.Text = "";
			txtLink19.Text = "";
			txtLink20.Text = "";
		}

		protected void btnCancel_Click(object sender, EventArgs e)
		{
			Clear();
		}
	}
}